package com.lyf.core.service.impl;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lyf.core.dao.DepartmentDao;
import com.lyf.core.pojo.Department;
import com.lyf.core.service.DeptService;

@Service
public class DeptServiceImpl implements DeptService {

	@Autowired
	private DepartmentDao deptDao;
	
	@Override
	public String add(Department dept) {
		
		if(dept == null || dept.getDepartment_name() == null){
			return "部门信息为空" ;
		}
		
		Department _dept = deptDao.selectPidAndDeptName(dept.getP_id(), dept.getDepartment_name());
		if(_dept != null){
			return "同级部门下该名称已经存在" ;
		}
		
		if(dept.getP_id() == null){
			dept.setP_id(0);
		}
		
		deptDao.insert(dept);
		
		return null;
	}

	@Override
	public String update(Department dept) {
		if(dept == null || dept.getDid() == null || dept.getDepartment_name() == null){
			return "部门信息为空" ;
		}
		
		Department _dept = deptDao.selectPidAndDeptName(dept.getP_id(), dept.getDepartment_name());
		if(_dept != null){
			return "同级部门下该名称已经存在" ;
		}
		
		deptDao.update(dept);
		return null;
	}

	@Override
	public String delete(int did) {
		// TODO Auto-generated method stub
		
		//判断是否有下级机构
		List<Department> childs = deptDao.findByPid(did);
		if(childs != null && !childs.isEmpty()){
			return "该机构存在下级机构,暂时不能删除" ;
		}
		
		deptDao.delete(did);
		return null;
	}

	@Override
	public Department findById(int did) {
		// TODO Auto-generated method stub
		return deptDao.selectById(did);
	}

	@Override
	public List<Department> findAll(boolean iterChilds) {
		
		List<Department> list = deptDao.findAll();
		if(iterChilds){
			list = childsDept(list);
		}
		
		return list;
	}

	
	/**
	  * 按照一级级的顺序排列菜单
	  * @param sources
	  * @return
	  */
	 public static List<Department> childsDept(List<Department> sources){
		 List<Department> orders = new LinkedList<>();
		 
		 for(Department m : sources)  { 
			 Integer p_id = m.getP_id();
			 
			 if(p_id == null || p_id == 0){
				 orders.add(m);
			 } 
		 }
		 
		 //子菜单组合
		//顶层钻取
		 for(Department m : orders) {
			 
			 itersDepts(sources, m);
		 }
		 
		 return orders ;
	 }
	 
	 private static void itersDepts(List<Department> sources , Department m) {
		 
		 if(m.getChildren() != null){
			 m.getChildren().clear();
		 }
		 
		 for(Department _m : sources) {
			 Integer p_id = _m.getP_id();
			 if(m.getDid().equals(p_id)) {
				 m.addChild(_m);
				 //下钻
			 }
		 }
		 
		 if(m.getChildren() != null && !m.getChildren().isEmpty()) {
			 
			 for(Department _m : m.getChildren()) {
				 itersDepts(sources, _m);
			 }
		 }  
	 }

	@Override
	public List<Department> findByParent(int pid ,boolean iterChilds) {
		// TODO Auto-generated method stub
		List<Department> list = deptDao.findByPid(pid);
		
		if(iterChilds){
			list = childsDept(list);
		}
		
		return list ;
		
	}
	
	
}
